<?php

namespace app\admin\validate\shop\order;

use app\shop\enums\order\OrderDeliveryType;
use think\Validate;

class Delivery extends Validate
{
    protected $rule = [
        'type'                => 'require',
        'delivery_company_id' => 'requireExpress',
        'order_id'            => 'require',
        'delivery_data'       => 'require',
    ];

    protected $message = [
        'type.require'                       => '缺少发货类型',
        'delivery_company_id.requireExpress' => '缺少物流公司',
        'delivery_no.requireExpress'         => '缺少快递单号',
        'order_id.require'                   => '缺少订单号',
        'delivery_data.require'              => '缺少发货信息'
    ];

    protected $scene = [
        'delivery' => ['delivery_data', 'type', 'delivery_company_id', 'delivery_no', 'order_id']
    ];

    protected function requireExpress($value, $rule, $data = []): bool
    {
        if ($data['type'] == OrderDeliveryType::WITHOUT_EXPRESS) {
            return true;
        }

        return !empty($value);
    }
}